/**
 * Copyright 2009 Joe LaPenna
 */

package com.joelapenna.foursquare.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.joelapenna.foursquare.types.Category;
import com.joelapenna.foursquare.types.Group;
import com.joelapenna.foursquare.types.Venue;

/**
 * @author Joe LaPenna (joe@joelapenna.com)
 */
public class VenueUtils {

    public static final boolean isValid(Venue venue) {
        return !(venue == null || venue.getId() == null || venue.getId().length() == 0);
    }

    public static final boolean hasValidLocation(Venue venue) {
        boolean valid = false;
        if (venue != null) {
            String geoLat = venue.getGeolat();
            String geoLong = venue.getGeolong();
            if (!(geoLat == null || geoLat.length() == 0 || geoLong == null || geoLong.length() == 0)) {
                if (geoLat != "0" || geoLong != "0") {
                    valid = true;
                }
            }
        }
        return valid;
    }
    
    public static final void removeHomes(Group<Venue> group) {
        for (int ix = group.size() - 1; ix >= 0; ix--) {
        	Venue v = group.get(ix);
        	Category c = v.getCategory();
        	
        	if (c == null)
        		continue;
        	
        	if (c.getNodeName().equals("Home"))
        		group.remove(ix);
        }
    }
    
    public static final void removeVenuesInForbiddenCategories(Group<Venue> group, ArrayList<String> categoryIds) {
    	for (int ix = group.size() - 1; ix >= 0; ix--) {
        	Venue v = group.get(ix);
        	Category c = v.getCategory();
        	
        	if (c == null)
        		continue;
        	
        	if (categoryIds.contains(c.getId()))
        		group.remove(ix);
        }
    }
    
    public static final void sortVenuesByDistance(Group<Venue> group, final boolean homeAndNoCategoriesToEnd) {
    	
        // Sort by distance. (C) loganjohnson, added optional getCategory null/home check
        Collections.sort(group, new Comparator<Venue>() {
            @Override
            public int compare(Venue a, Venue b) {
                int da = 0;
                int db = 0;
                if ( a.getDistance() != null ) {
                    try {
                        da = Integer.valueOf(a.getDistance());
                    } catch (NumberFormatException e) {
                    }
                }
                if ( b.getDistance() != null ) {
                    try {
                        db = Integer.valueOf(b.getDistance());
                    } catch (NumberFormatException e) {
                    }
                }
                
                if (homeAndNoCategoriesToEnd) {
	                if (    (a.getCategory() == null || a.getCategory().getNodeName().equals("Home")) && 
	                		(b.getCategory() != null && !b.getCategory().getNodeName().equals("Home"))
	                	)
	                	return 1;
	                
	                if (    (b.getCategory() == null || b.getCategory().getNodeName().equals("Home")) && 
	                		(a.getCategory() != null && !a.getCategory().getNodeName().equals("Home"))
	                	)
	                	return -1;
                }
                
                return da - db;
            }
        });

    }
}
